package org.jugger.tournament.gui.view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import org.jugger.tournament.model.Team;

import util.GUIUtils;
import util.I18N;
import util.SpringUtilities;

public class EditTeamPanel extends JPanel {

	private static final long serialVersionUID = 3838933381848994365L;
	private Team team;

	private JTextField teamName;
	private JTextField teamCity;
	private JCheckBox teamIsAdditionalTeam;

	private JButton buttonApply;
	private JButton buttonAbort;

	private ArrayList<EditTeamListener> editTeamListeners = new ArrayList<EditTeamListener>();

	public EditTeamPanel() {
		this(new Team());
	}

	public EditTeamPanel(Team team) {
		buildGUI();
		setTeam(team);
	}

	public void setTeam(Team team) {
		this.team = team;
		if(team == null) {
			teamName.setText("");
			teamCity.setText("");
			teamIsAdditionalTeam.setSelected(false);
		}
		else {
			teamName.setText(team.getName());
			teamCity.setText(team.getCity());
			teamIsAdditionalTeam.setSelected(team.getIsAdditionalTeam());
		}
	}

	public Team getTeam() {
		return team;
	}

	private void buildGUI() {

		// form

        JPanel form = new JPanel(new SpringLayout());
        JLabel labelName = new JLabel(I18N.__("teamName") + ":");
		JLabel labelCity = new JLabel(I18N.__("teamCity") + ":");
		JLabel labelIsAdditionalTeam = new JLabel(I18N.__("teamIsAdditionalTeam") + ":");
		teamName = GUIUtils.createTextField(20);
		teamCity = GUIUtils.createTextField(20);
		teamIsAdditionalTeam = GUIUtils.createCheckBox();

		form.add(labelName);
		labelName.setLabelFor(teamName);
		form.add(teamName);

		form.add(labelCity);
		labelCity.setLabelFor(teamCity);
		form.add(teamCity);

		form.add(labelIsAdditionalTeam);
		labelIsAdditionalTeam.setLabelFor(teamIsAdditionalTeam);
		form.add(teamIsAdditionalTeam);

		SpringUtilities.makeCompactGrid(form,
                                        3, 2,	//rows, cols
                                        6, 6,   //initX, initY
                                        6, 6);  //xPad, yPad

        // action buttons
        buttonApply = new JButton(I18N.__("apply"));
        buttonAbort = new JButton(I18N.__("abort"));
        JPanel buttons = GUIUtils.createXBoxLayoutPanel();
        buttons.add(Box.createGlue());
        buttons.add(buttonApply);
        buttons.add(buttonAbort);

        // main layout
        setLayout(new BorderLayout());
        add(BorderLayout.NORTH, form);
        add(BorderLayout.SOUTH, buttons);

		buttonApply.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				executeApply();
			}
		});
		buttonAbort.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				executeAbort();
			}
		});
	}

	private void executeApply() {
		Team newTeam = new Team(teamName.getText(), teamCity.getText(), teamIsAdditionalTeam.isSelected());
		if(newTeam.isValid()) {
			for(EditTeamListener l: editTeamListeners) {
				l.performTeamEdited(newTeam);
			}
		}
	}

	private void executeAbort() {
		for(EditTeamListener l: editTeamListeners) {
			l.performAbort();
		}
	}

	public void addEditTeamListener(EditTeamListener editTeamListener) {
		editTeamListeners.add(editTeamListener);
	}

	public interface EditTeamListener {
		public void performTeamEdited(Team team);
		public void performAbort();
	}
}
